package com.icloud.system.boot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.icloud.common.core.api.ResultCode;
import com.icloud.common.database.mapper.AdminUserMapper;
import com.icloud.common.database.model.AdminUser;
import com.icloud.common.web.enums.StateEnum;
import com.icloud.common.web.exception.Asserts;
import com.icloud.common.web.util.AuthUtil;
import com.icloud.system.boot.dao.AdminUserDao;
import com.icloud.system.boot.dto.AddAdminUserDto;
import com.icloud.system.boot.model.AdminUserAuth;
import com.icloud.system.boot.query.AdminUserQuery;
import com.icloud.system.boot.service.AdminUserAuthService;
import com.icloud.system.boot.service.AdminUserService;
import com.icloud.system.boot.vo.AdminUserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
 * 后台用户service 实现类
 */
@Service
public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUser> implements AdminUserService {
    @Autowired
    private AuthUtil authUtil;
    @Autowired
    private AdminUserAuthService adminUserAuthService;
    @Autowired
    private AdminUserDao adminUserDao;
    /**
     * 添加后台用户
     * @param dto
     */
    @Override
    @Transactional
    public void add(AddAdminUserDto dto) {
        Asserts.fail(StringUtils.isEmpty(dto.getPassword()),ResultCode.VALIDATE_FAILED);
        long count = count(new LambdaQueryWrapper<AdminUser>().eq(AdminUser::getUsername, dto.getUsername()));
        Asserts.fail(count > 0, ResultCode.USERNAME_REPETITION);

        AdminUser adminUser = new AdminUser();
        BeanUtil.copyProperties(dto,adminUser);

        adminUser.setState(StateEnum.NORMAL.getCode());
        save(adminUser);

        AdminUserAuth adminUserAuth = new AdminUserAuth();
        adminUserAuth.setAdminUserId(adminUser.getId());
        adminUserAuth.setAdminRoleId(dto.getAdminRoleId());
        adminUserAuth.setPassword(BCrypt.hashpw(dto.getPassword()));
        adminUserAuthService.save(adminUserAuth);
    }

    @Override
    public void edit(AddAdminUserDto dto) {
        Asserts.fail(null == dto.getId(),ResultCode.VALIDATE_FAILED);
        long count = count(new LambdaQueryWrapper<AdminUser>()
                .eq(AdminUser::getUsername, dto.getUsername())
                .ne(AdminUser::getId,dto.getId()));
        Asserts.fail(count > 0, ResultCode.USERNAME_REPETITION);

        AdminUser adminUser = getById(dto.getId());
        adminUser.setUsername(dto.getUsername());
        adminUser.setNickname(dto.getNickname());
        updateById(adminUser);

        AdminUserAuth adminUserAuth = adminUserAuthService.getOne(new LambdaQueryWrapper<AdminUserAuth>().eq(AdminUserAuth::getAdminUserId, dto.getId()));

        adminUserAuth.setAdminRoleId(dto.getAdminRoleId());

        if(StringUtils.isNotBlank(dto.getPassword())){
            adminUserAuth.setPassword(BCrypt.hashpw(dto.getPassword()));
        }
        adminUserAuthService.updateById(adminUserAuth);
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @Override
    public IPage<AdminUserVo> page(AdminUserQuery query) {
        LambdaQueryWrapper<AdminUser> wrapper = new LambdaQueryWrapper<AdminUser>()
                .like(StringUtils.isNoneBlank(query.getUsername()), AdminUser::getUsername, query.getUsername())
                .like(StringUtils.isNoneBlank(query.getNickname()), AdminUser::getNickname, query.getNickname());

        return adminUserDao.page(new Page<>(query.getCurrent(),query.getPageSize()),wrapper);
    }
}
